package utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author fred
 */
public class Security {

    public static String hashString(String frase) {
        byte[] bytes = generateHash(frase);
        
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            int high = ((bytes[i] >> 4) & 0xf) << 4;
            int low  = bytes[i] & 0xf;
            if (high == 0) {
                s.append('0');
            }
            s.append(Integer.toHexString(high | low));
        }
        return s.toString();
    }

    private static byte[] generateHash(String frase) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(frase.getBytes());
            return md.digest();
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }
    
}
